package com.huadi.cmcc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huadi.cmcc.domain.Mobilepackage;
import com.huadi.cmcc.domain.Result;
import com.huadi.cmcc.service.MobilepackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/mobile-package")
public class MobilePackageController {
    @Autowired
    private MobilepackageService mobilepackageService;

    /**
     * 添加移动套餐
     * @param mobilepackage
     * @return
     */
    @PostMapping("/create")
    public Result create(Mobilepackage mobilepackage) {
        mobilepackage.setId(null);
        mobilepackageService.save(mobilepackage);
        return Result.success();
    }

    /**
     * 获取套餐列表，移动套餐浏览
     * @param page
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Mobilepackage>> page(Page<Mobilepackage> page, String name) {
        Wrapper<Mobilepackage> query = Wrappers.<Mobilepackage>lambdaQuery()
                .like(StrUtil.isNotBlank(name), Mobilepackage::getName, name);
        final Page<Mobilepackage> resultPage = mobilepackageService.page(page, query);
        return Result.success(resultPage);
    }

    @GetMapping("list")
    public Result<List<Mobilepackage>> list() {
        final List<Mobilepackage> list = mobilepackageService.list();
        return Result.success(list);
    }

    @PostMapping("/update")
    public Result update(Mobilepackage mobilepackage) {
        mobilepackageService.updateById(mobilepackage);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        mobilepackageService.removeById(id);
        return Result.success();
    }
}
